#ifndef SHM_SEGMENT_H
#define SHM_SEGMENT_H

#include <sys/ipc.h> 

struct shm_segment {
    void *base_addr;
    int  shmid;
    bool owner;
    char ch;
    shm_segment(char ch = '0') : base_addr(nullptr), shmid(-1), owner(true), ch(ch) {}
    ~shm_segment() { if (owner && shmid != -1) this->fini(); }

    int init(key_t key, size_t size, bool resum) { return !resum ? __create(key, size) : __attach(key); }
    int __create(key_t key, size_t size);
    int __attach(key_t key);
    void fini();

    /**
     * if the shared memory is released from this segment, it
     * will not be destroyed when the segment is destructed.
     * also you should this things by youself;
     */
    void release() { this->owner = false; } 
    void *address() { return this->base_addr; } 
};

#endif // SHM_SEGMENT_H 
